package com.booksystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.booksystem.dto.CustomerServiceTicketAddDTO;
import com.booksystem.dto.CustomerServiceTicketQueryDTO;
import com.booksystem.dto.CustomerServiceTicketReplyDTO;
import com.booksystem.dto.CustomerServiceTicketUpdateStatusDTO;
import com.booksystem.entity.CustomerServiceTicketDO;
import com.booksystem.exception.BusinessException;
import com.booksystem.mapper.CustomerServiceTicketMapper;
import com.booksystem.mapper.UserInfoMapper;
import com.booksystem.service.CustomerServiceTicketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *   客服工单 Service 实现类
 * </p>
 * @author 段伟豪
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerServiceTicketServiceImpl implements CustomerServiceTicketService {

    @Autowired
    private CustomerServiceTicketMapper customerServiceTicketMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Boolean submitTicket(CustomerServiceTicketAddDTO addDTO) {
        // 校验用户是否存在
        if (userInfoMapper.selectById(addDTO.getUserId()) == null) {
            throw new BusinessException("000001", "用户信息不存在");
        }

        // 构建工单对象并保存
        CustomerServiceTicketDO ticketDO = new CustomerServiceTicketDO();
        ticketDO.setUserId(addDTO.getUserId());
        ticketDO.setTitle(addDTO.getTitle());
        ticketDO.setContent(addDTO.getContent());
        ticketDO.setPriority(addDTO.getPriority() != null ? addDTO.getPriority() : 1); // 默认为低优先级
        ticketDO.setStatus(1); // 初始状态为待处理
        ticketDO.setCreateTime(new Date());
        ticketDO.setUpdateTime(new Date());

        return customerServiceTicketMapper.insert(ticketDO) > 0;
    }

    @Override
    public PageInfo<CustomerServiceTicketDO> getTicketsList(CustomerServiceTicketQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getSize());
        QueryWrapper<CustomerServiceTicketDO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", queryDTO.getUserId());
        if (queryDTO.getStatus() != null) {
            wrapper.eq("status", queryDTO.getStatus());
        }
        List<CustomerServiceTicketDO> list = customerServiceTicketMapper.selectList(wrapper);
        return new PageInfo<>(list);
    }

    @Override
    public Boolean updateTicketStatus(CustomerServiceTicketUpdateStatusDTO updateStatusDTO) {
        // 判断工单是否存在
        CustomerServiceTicketDO existTicket = customerServiceTicketMapper.selectById(updateStatusDTO.getTicketId());
        if (existTicket == null) {
            throw new BusinessException("000001", "工单信息不存在");
        }

        // 更新工单状态
        existTicket.setStatus(updateStatusDTO.getStatus());
        existTicket.setUpdateTime(new Date());
        return customerServiceTicketMapper.updateById(existTicket) > 0;
    }

    @Override
    public Boolean replyTicket(CustomerServiceTicketReplyDTO replyDTO) {
        // 判断工单是否存在
        CustomerServiceTicketDO existTicket = customerServiceTicketMapper.selectById(replyDTO.getTicketId());
        if (existTicket == null) {
            throw new BusinessException("000001", "工单信息不存在");
        }

        // 更新工单的回复内容
        existTicket.setResponseContent(replyDTO.getResponseContent());
        existTicket.setUpdateTime(new Date());
        return customerServiceTicketMapper.updateById(existTicket) > 0;
    }
}